package m3;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
//        新建消费者实例
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumer3");
//        设置name server
        c.setNamesrvAddr("192.168.64.141:9876");
//        订阅消息
        /** 标签
         *  * ---所有标签
         *  tag1  --- 只接收tag1标签
         *  Tag1 || Tag2 || Tag3 ---接收Tag1,Tag2,Tag3的消息
         */
        c.subscribe("Topic3", "*");
//        设置消息监听器
        /**
         * 启动多个线程，并发的处理消息
         */
        c.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg : msgs) {
                    String s = new String(msg.getBody());
                    System.out.println("收到:"+s);
                }
//               返回消息处理状态(回执)
//                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                System.out.println("消息处理失败");
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;//通知服务器，稍后重新投递这条消息
            }
        });
//        启动
        c.start();
    }
}
